package com.demomaster.quickcompat.widget.dialog;

import static android.view.View.OVER_SCROLL_NEVER;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demomaster.quickcompat.R;
import com.demomaster.quickcompat.drawable.DividerGravity;
import com.demomaster.quickcompat.drawable.QDividerDrawable;
import com.demomaster.quickcompat.listener.OnSingleItemClickListener;
import com.demomaster.quickcompat.util.DisplayUtil;
import com.demomaster.quickcompat.widget.base.QuickGravity;
import com.demomaster.quickcompat.widget.dialog.adapter.SheetAdapter;

import java.util.Arrays;
import java.util.List;


/**
 * 多选
 * @author squirrel桓
 * @date 2019/1/7.
 * description：
 */
public class QDMulSheetDialog extends QuickDialog {
    private List<String> data;
    private int columnCount = 1;
    private ShowType showType = ShowType.List;
    private OnDialogActionListener onDialogActionListener;
    private int boxColor;

    public QDMulSheetDialog(Context context, Builder builder) {
        super(context,builder);
        data = builder.data;
        columnCount = builder.columnCount;
        showType = builder.showType;
        onDialogActionListener = builder.onDialogActionListener;
        boxColor = builder.boxColor;
        init();
    }

    public QDMulSheetDialog(Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

//    protected QDMulSheetDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
//        super(context, cancelable, cancelListener);
//        init();
//    }

    @Override
    public boolean isCanSliding() {
        return true;
    }

    private void init() {
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        initData();
    }

    RecyclerView recyclerView;
    private void initData() {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = gravity;

        //RecyclerView recyclerView = new RecyclerView(getContext());
        recyclerView = findViewById(R.id.recycler);
        recyclerView.setOverScrollMode(OVER_SCROLL_NEVER);
        switch (showType) {
            case List:
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                linearLayoutManager.setAutoMeasureEnabled(true);
                recyclerView.setLayoutManager(linearLayoutManager);
                break;
            case Grid:
                recyclerView.setLayoutManager(new GridLayoutManager(getContext(), columnCount));
                break;
        }

        SheetAdapter adapter = new SheetAdapter(getContext(), data);
        adapter.setOnItemClickListener(new OnSingleItemClickListener() {
            @Override
            public void onItemClickEvent(AdapterView<?> parent, View view, int position, long id) {
                onDialogActionListener.onItemClick(QDMulSheetDialog.this, position, data);
            }
        });

        recyclerView.setAdapter(adapter);
        //relativeLayout.setBackgroundColor(boxColor);
        QDividerDrawable drawable_bg = new QDividerDrawable(DividerGravity.NONE);
        float c = DisplayUtil.dip2px(getContext(), 20);
        drawable_bg.setCornerRadii(new float[]{c, c, c, c, 0, 0, 0, 0});
        drawable_bg.setBackGroundColor(boxColor);
        //setPanelMaginTop(DisplayUtil.getStatusBarHeight(getContext()) + DisplayUtil.getActionBarHeight(getContext()));
    }

    public enum ShowType {
        List, Grid
    }

    public static class Builder extends QuickDialog.Builder<Builder> {
        private List<String> data;
        private int columnCount = 1;
        private ShowType showType = ShowType.List;
        private OnDialogActionListener onDialogActionListener;
        private int boxColor = Color.WHITE;

        public Builder(Context context) {
            super(context);
            animationStyleID = R.style.keybored_anim;
            quickGravity = QuickGravity.BOTTOM;
            isFullScreen = true;
            setContentView(R.layout.quick_dialog_share_default);
        }

        public Builder setData(List<String> data) {
            this.data = data;
            return this;
        }

        public Builder setData(String[] data) {
            this.data = Arrays.asList(data);
            return this;
        }

        public Builder setShowType(ShowType showType) {
            this.showType = showType;
            return this;
        }

        public Builder setBoxColor(int backgroundColor) {
            this.boxColor = backgroundColor;
            return this;
        }

        public Builder setColumnCount(int columnCount) {
            this.columnCount = columnCount;
            return this;
        }

        public Builder setOnDialogActionListener(OnDialogActionListener onDialogActionListener) {
            this.onDialogActionListener = onDialogActionListener;
            return this;
        }

        public QDMulSheetDialog create() {
            return new QDMulSheetDialog(context, this);
        }
    }

    public interface OnDialogActionListener {
        void onItemClick(QDMulSheetDialog dialog, int position, List<String> data);
    }
}
